package com.jie.deletefile.Location;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.jie.deletefile.Permission.CheckPermission;
import com.jie.deletefile.Permission.PermissionActivity;
import com.jie.deletefile.R;

public class LocationActivity extends AppCompatActivity implements View.OnClickListener {

    private android.widget.Button btnlocation;
    private android.widget.TextView tv1;
    private android.widget.TextView tv2;
    private android.widget.LinearLayout activitylocation;

    private static final int REQUEST_CODE = 1;
    private static final String[] PERMISSION = new String[]{
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.ACCESS_FINE_LOCATION,
    };
    private boolean hadpermission;
    private CheckPermission checkPermission;
    private String TAG = "TAG";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_location);
        this.activitylocation = (LinearLayout) findViewById(R.id.activity_location);
        this.tv2 = (TextView) findViewById(R.id.tv2);
        this.tv1 = (TextView) findViewById(R.id.tv1);
        this.btnlocation = (Button) findViewById(R.id.btn_location);
        checkPermission = new CheckPermission(this);
        btnlocation.setOnClickListener(this);
        if (checkPermission.permissionSet(PERMISSION)) {
            startPermissionActivity();
            hadpermission = false;
        } else {
            hadpermission = true;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE && requestCode == PermissionActivity.PERMISSION_DENIEG) {
            hadpermission = false;
        } else {
            hadpermission = true;
        }
    }

    private void startPermissionActivity() {
        PermissionActivity.startActivityForResult(LocationActivity.this, REQUEST_CODE, PERMISSION);
    }

    @Override
    public void onClick(View v) {
        if (!hadpermission) {
            Toast.makeText(this, "缺少权限", Toast.LENGTH_SHORT).show();
            return;
        }
        if (getLocation(LocationActivity.this) != null) {
            Log.e("TAG", "onClick: 111111");

            tv1.setText("经度 " + getLocation(LocationActivity.this).getLongitude());
            tv2.setText("维度 " + getLocation(LocationActivity.this).getLatitude());
        }
        Log.e("TAG", "onClick: 22222222222222");
    }

    private Location getLocation(Context context) {
        LocationManager locationManager = (LocationManager) context
                .getSystemService(context.LOCATION_SERVICE);
        Log.e(TAG, "getLocation: locationManager==null" + (locationManager == null));
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
//            return TODO;
        }
        Location location = locationManager.getLastKnownLocation(locationManager.GPS_PROVIDER);
                 if (location == null) {
                        //获取NETWORK支持
                         location = locationManager.getLastKnownLocation(locationManager.NETWORK_PROVIDER);
                    }
                 return location;
             }
}
